package com.lightboat.web.controller.crm;

import com.lightboat.common.annotation.Anonymous;
import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.page.TableDataInfo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.common.utils.poi.ExcelUtil;
import com.lightboat.system.domain.CrmCity;
import com.lightboat.system.domain.vo.CrmCityTreeVo;
import com.lightboat.system.service.ICrmCityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 城市Controller
 * 
 * @author lightboat
 * @date 2025-08-21
 */
@Tag(name = "城市")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/city")
public class CrmCityController extends BaseController {

    private final ICrmCityService crmCityService;

    /**
     * 查询城市列表
     */
    @Operation(summary = "查询城市列表")
    @PreAuthorize("@ss.hasPermi('system:city:list')")
    @GetMapping("/list")
    public TableDataInfo<CrmCity> list(CrmCity crmCity)
    {
        startPage();
        List<CrmCity> list = crmCityService.selectCrmCityList(crmCity);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 导出城市列表
     */
    @Operation(summary = "导出城市列表")
    @PreAuthorize("@ss.hasPermi('system:city:export')")
    @Log(title = "城市", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmCity crmCity)
    {
        List<CrmCity> list = crmCityService.selectCrmCityList(crmCity);
        ExcelUtil<CrmCity> util = new ExcelUtil<CrmCity>(CrmCity.class);
        util.exportExcel(response, list, "城市数据");
    }

    /**
     * 获取城市详细信息
     */
    @Operation(summary = "获取城市详细信息")
    @PreAuthorize("@ss.hasPermi('system:city:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(crmCityService.selectCrmCityById(id));
    }

    /**
     * 新增城市
     */
    @Operation(summary = "新增城市")
    @PreAuthorize("@ss.hasPermi('system:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmCity crmCity)
    {
        return toAjax(crmCityService.insertCrmCity(crmCity));
    }

    /**
     * 修改城市
     */
    @Operation(summary = "修改城市")
    @PreAuthorize("@ss.hasPermi('system:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCity crmCity)
    {
        return toAjax(crmCityService.updateCrmCity(crmCity));
    }

    /**
     * 删除城市
     */
    @Operation(summary = "删除城市")
    @PreAuthorize("@ss.hasPermi('system:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(crmCityService.deleteCrmCityByIds(ids));
    }


    /**
     * 获取tree结构的列表
     */
    @Anonymous
    @Operation(summary = "获取tree结构的列表")
    @GetMapping( "/list/tree")
    public TableDataInfo<List<CrmCityTreeVo>> getListTree() {
        return getDataTable(crmCityService.getListTree());
    }
}
